import { _decorator, Collider2D, Component, Contact2DType, instantiate, IPhysics2DContact, JsonAsset, misc, Node, Prefab, RigidBody2D, v2, v3, Vec2 } from 'cc';
import { Constant } from '../AshFramework/Datas/Constant';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { GameManager } from './GameManager';
import { BulletHit } from './BulletHit';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { EnemyController } from './EnemyController';
import PlayerController from '../PlayerController';
const { ccclass, property } = _decorator;

@ccclass('BulletController')
export class BulletController extends Component {

    @property
    Speed: number = 10;

    RigidBody2D: RigidBody2D = null;
    Collider2D: Collider2D = null;

    Name: string = "";

    DirX: number = 0;
    DirY: number = 0;

    Harm: number = 0;
    ArmorPenetration: number = 0;
    IsRemove: boolean = false;

    protected onLoad(): void {
        this.RigidBody2D = this.getComponent(RigidBody2D);
        this.Collider2D = this.getComponent(Collider2D);

        if (this.Collider2D) {
            this.Collider2D.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    fire(bulletName: string, dirX: number, dirY: number) {
        this.Name = bulletName;
        this.DirX = dirX;
        this.DirY = dirY;
        ResourceUtil.LoadJson("AmmoData").then((jsonAsset: JsonAsset) => {
            const json = jsonAsset.json[this.Name];
            this.Harm = json.harm;
            this.ArmorPenetration = json.armorPenetration;
        })

        let angleRadians = Math.atan2(this.DirY, this.DirX);
        let angleDegrees = misc.radiansToDegrees(angleRadians);

        this.node.angle = angleDegrees;
    }

    protected update(dt: number): void {
        if (this.RigidBody2D) {
            const v = new Vec2(this.DirX * this.Speed, this.DirY * this.Speed);
            this.RigidBody2D.linearVelocity = v;
        }
    }

    subArmor(sub: number) {
        this.ArmorPenetration -= sub;
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        if (this.IsRemove) return;
        if (otherCollider.group == Constant.Group.ENEMY) {
            otherCollider.node.getComponent(EnemyController).hit(this.Harm, this.ArmorPenetration)
            this.removeSelf();
        } else if (otherCollider.group == Constant.Group.PLAYER) {
            PlayerController.Instance.hit(this.Harm);
            this.removeSelf();
        } else if (otherCollider.group == Constant.Group.MAP) {
            this.removeSelf();
        }
    }

    removeSelf() {
        this.IsRemove = true;
        const worldPos = this.node.getWorldPosition().clone();
        const angle = this.node.angle;
        BundleManager.LoadPrefab(Bundles.Prefabs, "子弹击中").then((prefab: Prefab) => {
            const hit = instantiate(prefab);
            hit.parent = GameManager.Instance.Canvas;
            hit.getComponent(BulletHit).show(worldPos, angle - 90);
        })
        this.scheduleOnce(() => { this.node.destroy(); })
    }

}


